package cn.kgc.springboot.shiro.config;

import cn.kgc.springboot.shiro.entity.Role;
import cn.kgc.springboot.shiro.entity.User;
import cn.kgc.springboot.shiro.entity.UserRole;
import cn.kgc.springboot.shiro.mapper.RoleMapper;
import cn.kgc.springboot.shiro.mapper.UserMapper;
import cn.kgc.springboot.shiro.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mosin
 * @version: v1.0  2023/1/10
 * 自定义realm
 */
public class CustomerRealm  extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper  userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;
    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("---------doGetAuthorizationInfo-----------");
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        // 获取用户的身份信息  username
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
        // 根据用户的身份信息 查询用户的角色信息
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", primaryPrincipal);
        User user = userMapper.selectOne(userQueryWrapper);
        //获取用户id
        Integer userId = user.getId();

        //根据userId查询user_role表  userRole

        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id", userId);
        //  select  *  from  user_role  where user_id = userId
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);

        //获取角色id
        List<Integer> roleIds = userRoles.stream().map(userRole -> {
            return userRole.getRoleId();
        }).collect(Collectors.toList());
        System.out.println("roleIds = " + roleIds);
        // 根据角色id查询角色名称
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.in("id", roleIds);
        // select *  from sys_role  where id in (1,2)
        List<Role> roles = roleMapper.selectList(roleQueryWrapper);

        // 获取所有角色名
        List<String> roleNames = roles.stream().map(role -> {
            return role.getRolename();
        }).collect(Collectors.toList());

        System.out.println("roleNames = " + roleNames);
        // 授权
        simpleAuthorizationInfo.addRoles(roleNames);

        return simpleAuthorizationInfo;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)token;
        String username = usernamePasswordToken.getUsername();

        // 根据用户名查询用户信息
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);   //  select  *  from sys_user  where username = username
        User user = userMapper.selectOne(userQueryWrapper);
        if(user!=null){
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes(user.getSalt()), this.getName());
            return simpleAuthenticationInfo;
        }
        return null;
    }
}
